package com.aiiter.house.service.impl;

import com.aiiter.house.domain.vo.HomeStatistics;
import com.aiiter.house.mapper.HouseBillingInformationMapper;
import com.aiiter.house.mapper.HouseContractMapper;
import com.aiiter.house.mapper.HouseRoomInformationMapper;
import com.aiiter.house.service.HouseAppIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class HouseAppIndexServiceImpl implements HouseAppIndexService {

    @Resource
    HouseBillingInformationMapper billingInformationMapper;
    @Resource
    HouseRoomInformationMapper roomInformationMapper;
    @Resource
    HouseContractMapper contractMapper;

    @Override
    public HomeStatistics getHomeStatistics() {
//        本月账单和今日账单
        HomeStatistics homeStatistics = billingInformationMapper.selectAllBill();

        // 使用方法封装重复的逻辑
        //查询本月收入
        homeStatistics.setIncomeMonth(getStringValue(billingInformationMapper.selectTncomeMonth()));
        //闲置房间
        homeStatistics.setSpareRoom(getStringValue(roomInformationMapper.selectSpareRoom()));
        //到期租户
        homeStatistics.setExpiredTenant(getStringValue(contractMapper.selectExpiredTenant()));

        return homeStatistics;
    }

    // 辅助方法，用于处理可能为null的值
    private String getStringValue(Number number) {
        return Optional.ofNullable(number)
                .map(String::valueOf)
                .orElse("0");
    }

}
